package com.noahplat.person.service.impl;

import com.noahplat.common.utils.Result;
import com.noahplat.common.utils.StatusCode;
import com.noahplat.person.mapper.EhrsMapper;
import com.noahplat.person.mapper.PersonBaseMapper;
import com.noahplat.person.mapper.PersonInfoMapper;
import com.noahplat.person.pojo.Ehrs;
import com.noahplat.person.pojo.PersonBase;
import com.noahplat.person.pojo.PersonInfo;
import com.noahplat.person.service.EmergencyService;
import com.noahplat.person.utils.EmergencyDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

/**
 * @ClassName: EmergencyServiceImpl
 * @Description: 急救卡实现类
 * @Author 李景星
 * @Date 2021/3/8
 * @Version 1.0
 */
@Service
public class EmergencyServiceImpl implements EmergencyService {


    @Autowired
    private PersonInfoMapper personInfoMapper;
    @Autowired
    private PersonBaseMapper personBaseMapper;
    @Autowired
    private EhrsMapper ehrsMapper;


    /**
     * 获取人员急救卡信息
     * @param pid
     * @return
     */
    @Override
    public Result getEmergency(Long pid) {
        //返回前端dto
        EmergencyDto emergencyDto = new EmergencyDto();
        //获取人员基本信息
        Example personBaseExample = new Example(PersonBase.class);
        personBaseExample.createCriteria().andEqualTo("pid", pid);
        PersonBase personBase = personBaseMapper.selectOneByExample(personBaseExample);
        if(!ObjectUtils.isEmpty(personBase)){
            emergencyDto.setAbotype(personBase.getAbotype());
        }
        //获取人员医疗记录
        Example ehrsExample = new Example(Ehrs.class);
        ehrsExample.createCriteria().andEqualTo("pid", pid);
        Ehrs ehrs = ehrsMapper.selectOneByExample(ehrsExample);
        if(!ObjectUtils.isEmpty(ehrs)){
            emergencyDto.setAnamnesis(ehrs.getAnamnesis());
            emergencyDto.setAnaphylaxis(ehrs.getAnaphylaxis());
            emergencyDto.setCdrug(ehrs.getCdrug());
            emergencyDto.setFhistory(ehrs.getFhistory());
        }
        //获取人员详细信息
        Example personInfoExample = new Example(PersonInfo.class);
        personInfoExample.createCriteria().andEqualTo("pid", pid);
        PersonInfo personInfo = personInfoMapper.selectOneByExample(personInfoExample);
        if(!ObjectUtils.isEmpty(personInfo)){
            emergencyDto.setCname(personInfo.getCname());
            emergencyDto.setCphone(personInfo.getCphone());
        }
        return new Result(true, StatusCode.OK, "查询成功", emergencyDto);
    }
}
